<?php
	class Parsers
	{
		/**
		* Parses url into controller, action and parameters
		*
		* @param string $url: url to be parsed
		* @param string $exclude_list: array list of words to exclude
		* @return array parsed url(controller, action and parameters)
		*/	
		function ParseURL($url, $exclude_list)
		{
			$url = str_replace('_', '', $url);
			$url = str_replace('/spanel', '', $url);
			if ($url == null)
			{
				$out['action'] = null;
				$out['controller'] = null;
				$out['parameters'] = null;
				$out['module_name'] = null;
			}

			if ($url && ('/' != $url[0]))
			{
				if (!defined('SERVER_IIS'))
				{
					$url = '/' . $url;
				}
			}
			$out = array();
			$r = null;
			$default_route = array('/:controller/:action/* (default)',
								   '/^(?:\/(?:([a-zA-Z0-9_\\-\\.]+)(?:\\/([a-zA-Z0-9_\\-\\.]+)(?:[\\/\\?](.*))?)?))[\\/]*$/',
								   array('controller', 'action'), array());
			$routes[] = $default_route;
			if (strpos($url, '?') !== false)
			{
				$url = substr($url, 0, strpos($url, '?'));
			}
			foreach($routes as $route)
			{
				list($route, $regexp, $names, $defaults) = $route;
				if (preg_match($regexp, $url, $r))
				{
					array_shift ($r);
					foreach($names as $name)
					{
						$out[$name] = null;
					}
					$ii=0;
					if (is_array($defaults))
					{
						foreach($defaults as $name => $value)
						{
							if (preg_match('#[a-zA-Z_\-]#i', $name))
							{
								$out[$name] = $value;
								$out['parameters'] = null;
							}
							else
							{
								$out['parameters'][] = $value;
							}
						}
					}
					foreach($r as $found)
					{
						if (empty($exclude_list)) $exclude_list = array();
						if (!(in_array($found, $exclude_list)))
						{
							if (isset($names[$ii]))
							{
								$out[$names[$ii]] = str_replace("/", "", $found);
								$out['parameters'] = null;
							}
							else
							{
								$out['parameters'] = $this->Cleanup(explode('/', $found));
							}
							$ii++;
						}
					}
					break;
				}
			}
			return $out;
		}

		/**
		* Makes the value empty
		*
		* @param any $value value to be made empty
		* @return mixed $out empty $value
		*/	
		function Cleanup($value)
		{
			$out = is_array($value) ? array(): null;
			foreach($value as $k => $v)
			{
				if ($v == "0")
				{
					$out[$k] = $v;
				}
				elseif ($v)
				{
					$out[$k] = $v;
				}
			}
			return $out;
		}

		/**
		* Parses file content into array
		*
		* @param string $fileName file to be parsed
		* @return array $setting paersed file
		*/	
		function ParseFile($fileName)
		{
			$fileLineArray = file($fileName);
			foreach($fileLineArray as $fileLine)
			{
				$dataLine = trim($fileLine);
				$firstChar = substr($dataLine, 0, 1);
				if ($firstChar != ';' && $dataLine != '')
				{
					if ($firstChar == '[' && substr($dataLine, -1, 1) == ']')
					{
					}
					else
					{
						$delimiter = strpos($dataLine, '=');
						if ($delimiter > 0) {
							$key = strtolower(trim(substr($dataLine, 0, $delimiter)));
							$value = trim(substr($dataLine, $delimiter + 1));

							if (substr($value, 0, 1) == '"' && substr($value, -1) == '"')
							{
								$value = substr($value, 1, -1);
							}

							$setting[$key] = stripcslashes($value);
						}
						else
						{
							$setting[strtolower(trim($dataLine))] = '';
						}
					}
				}
				else
				{
				}
			}
			return $setting;
		}
	}
?>